import ctypes
import config
import time

DD_drive = config.resource_path("DD43390.dll")

# 加载DD驱动
try:
    dd = ctypes.windll.LoadLibrary(DD_drive)  # 替换为你的 DLL 路径
    dd_code = dd.DD_btn(0)  # 初始化 DD
    if dd_code != 1:
        raise Exception("DD 驱动初始化失败，请检查驱动是否正确安装！")
except Exception as e:
    dd = None
    print(str(e))

# 按键模拟
def simulate_key(key_code):
    try:
        # 按下按键
        dd.DD_key(key_code, 1)  # 按下键
        time.sleep(0.001)
        dd.DD_key(key_code, 2)  # 松开键
    except ValueError:
        print("请输入有效的键码！")
    except Exception as e:
        print(f"模拟失败: {e}")

# 鼠标模拟
def simulate_btn(key_code):
    try:
        # 按下按键
        dd.DD_btn(key_code, 1)  # 按下键
        time.sleep(0.001)
        dd.DD_btn(key_code*2, 2)  # 松开键
    except ValueError:
        print("请输入有效的键码！")
    except Exception as e:
        print(f"模拟失败: {e}")

# 定义键名到 dd 键码的映射
dd_key = {
    "esc": 100, "f1": 101, "f2": 102, "f3": 103, "f4": 104, "f5": 105, "f6": 106, "f7": 107, "f8": 108, "f9": 109, "f10": 110, "f11": 111, "f12": 112,
    "`": 200, "1": 201, "2": 202, "3": 203, "4": 204, "5": 205, "6": 206, "7": 207, "8": 208, "9": 209, "0": 210, "-": 211, "+": 212, "\\": 213, "backspace": 214,
    "tab": 300, "q": 301, "w": 302, "e": 303, "r": 304, "t": 305, "y": 306, "u": 307, "i": 308, "o": 309, "p": 310, "[": 311, "]": 312, "enter": 313,
    "caps lock": 400, "a": 401, "s": 402, "d": 403, "f": 404, "g": 405, "h": 406, "j": 407, "k": 408, "l": 409, ";": 410, "'": 411,
    "shift": 500, "z": 501, "x": 502, "c": 503, "v": 504, "b": 505, "n": 506, "m": 507, ",": 508, ".": 509, "/": 510, 
    "ctrl": 600, "win": 601, "alt": 602, "space": 603,
    "up": 709, "left": 710, "down": 711, "right": 712,
    "num0": 800, "num1": 801, "num2": 802, "num3": 803, "num4": 804, "num5": 805, "num6": 806, "num7": 807, "num8": 808, "num9": 809,
    "num": 810, "num/": 811, "num*": 812, "num-": 813, "num+": 814, "numenter": 815, "num.": 816,
    "lb": 1, "rb": 4,"mid": 16, "mx":64, "mx2": 256,
}